package database;

import Entity.Author;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.AuthorManager;

/**
 *
 * @author BinhNV_A04911
 */
public class AuthorManagerDB {

    private CallableStatement cstmt;
    private Statement stmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public AuthorManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(AuthorManagerDB authorManager) {
        try {
            stmt = conn.createStatement();
            cstmt = conn.prepareCall("{call sp_GetAccount}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
                String authorName = rs.getString("AuthorName");
               
                int authorID = rs.getInt("AuthorID");
               
                Author author = new Author(authorID,authorName);
                authorManager.add(author);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(AuthorManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteAuthor(Author obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteAuthor(?)}");
            cstmt.setInt(1,obj.getAuthorID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(AuthorManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertAuthor(Author obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertAuthor(?,?)}");
            cstmt.setString(1, obj.getAuthorName());
            cstmt.setInt(2, obj.getAuthorID());
           
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(AuthorManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateAuthort(Author obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateAccount(?,?)}");
            cstmt.setString(1, obj.getAuthorName());
           
            cstmt.setInt(2, obj.getAuthorID());
      
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(AuthorManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    }

